package com.elong.android.specialhouse.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.elong.android.specialhouse.ApartmentAPI;
import com.elong.android.specialhouse.AppConstants;
import com.elong.android.specialhouse.R;
import com.elong.android.specialhouse.account.Account;
import com.elong.android.specialhouse.base.BaseVolleyActivity;
import com.elong.android.specialhouse.request.PublishEvaluationReq;
import com.elong.android.specialhouse.ui.FlexibleRatingBar;
import com.elong.android.specialhouse.utils.DateTimeUtils;
import com.elong.android.specialhouse.utils.EVerifyString;
import com.elong.android.specialhouse.utils.ImageLoaderUtils;
import com.elong.android.specialhouse.utils.ToastUtils;
import com.elong.framework.netmid.ElongRequest;
import com.elong.framework.netmid.response.IResponse;
import com.elong.framework.netmid.response.StringResponse;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * 发表评价页
 * 
 * @author cheng.wang
 * @date 2015/9/16 18:39
 */
public class PublishEvaluationActivity extends BaseVolleyActivity<IResponse<?>> {
	public static final String ORDERDETAIL = "order_detail";
	private static final int EVALUATION_MAX_COUNT = 500;
	private TextView tvSubmit;
	private EditText etContent;
	private PublishEvaluationReq mRequest;

	private FlexibleRatingBar ratingBar;

	private String mContent;
	private TextView tvApartmentName;// 公寓名称
	private TextView tvTime;// 入住时间
	private ImageView ivPhoto;

	@Override
	protected void initContentView() {
		setContentView(R.layout.act_publish_evaluation);
		setHeader(R.string.title_publish_evaluation);

		ratingBar = (FlexibleRatingBar) findViewById(R.id.rating_bar);
		tvSubmit = (TextView) findViewById(R.id.tv_submit);
		etContent = (EditText) findViewById(R.id.et_evaluation_content);
		tvApartmentName = (TextView) findViewById(R.id.tv_title);
		tvTime = (TextView) findViewById(R.id.tv_time);
		ivPhoto = (ImageView) findViewById(R.id.iv_photo);

		tvSubmit.setOnClickListener(this);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initData();
	}

	private void initData() {
		String apartmentName = getIntent().getStringExtra(
				AppConstants.APARTMENT_NAME);
		tvApartmentName.setText(apartmentName);

		String checkinTime = getIntent().getStringExtra(
				AppConstants.CHECKIN_DATE_TIME);
		String checkoutTime = getIntent().getStringExtra(
				AppConstants.CHECKOUT_DATE_TIME);
		tvTime.setText(DateTimeUtils.changeDateFormat(checkinTime) + "-"
				+ DateTimeUtils.changeDateFormat(checkoutTime));

		String imageUrl = getIntent().getStringExtra(
				AppConstants.HOUSE_IAMGE_URL);
		if (!TextUtils.isEmpty(imageUrl)) {
			ImageLoader.getInstance().displayImage(imageUrl, ivPhoto,
					ImageLoaderUtils.getHouseImageOptions());
		}
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);

		switch (v.getId()) {
		case R.id.tv_submit:
			if (validate()) {
				setupRequestParams();
				sendRequest();
			}
			break;
		}

	}

	private boolean validate() {
		mContent = etContent.getText().toString().trim();
		if (ratingBar.getRating() < 1) {
			ToastUtils.show(this, R.string.rate_can_not_be_null);
			return false;
		}
		if (TextUtils.isEmpty(mContent)) {
			ToastUtils.show(this, R.string.evaluation_can_not_be_null);
			return false;
		}

		if (EVerifyString.containsLimitedWords(mContent)) {
			ToastUtils.show(this, R.string.evaluation_limited);
			return false;
		}
		if (mContent.length() > EVALUATION_MAX_COUNT) {
			ToastUtils.show(this, R.string.evaluation_too_long);
			return false;
		}

		return true;
	}

	private void sendRequest() {
		requestHttp(mRequest, ApartmentAPI.publishEvaluation, StringResponse.class, true);
	}

	private void setupRequestParams() {
		mRequest = new PublishEvaluationReq();
		mRequest.HouseId = getIntent().getLongExtra(AppConstants.HOUSE_ID, 0);
		mRequest.OrderId = getIntent().getStringExtra(AppConstants.ORDER_ID);
		mRequest.GorderId = getIntent().getStringExtra(AppConstants.GORDER_ID);
		int rate = (int) ratingBar.getRating();
		mRequest.Star = rate;
		mRequest.Content = mContent;
		mRequest.SenderId = Long.parseLong(Account.getInstance().getUserId());
	}

	@Override
	public void onTaskPost(ElongRequest request, IResponse<?> t) {
		super.onTaskPost(request, t);

		ApartmentAPI action = (ApartmentAPI) request.getRequestOption()
				.getHusky();
		JSONObject result = JSON.parseObject(((StringResponse) t).getContent());
		if (result == null) {
			ToastUtils.show(this, R.string.server_error);
			return;
		}


		// 过滤错误信息
		if (handleIsError(request, result)) {
			return;
		}

		// 返回成功
		switch (action) {
		case publishEvaluation:
			goToPublishSucceedPage();
			break;
		}
	}

	private void goToPublishSucceedPage() {
		Intent intent = new Intent(this, EvaluationSucceedActivity.class);
		startActivity(intent);
		finish();
	}
}